package com.base.kongjian.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import com.base.kongjian.dao.WorksNumCountDao;
import com.base.kongjian.entity.WorksNumCount;
import com.base.kongjian.service.WorksNumCountService;

import cn.mysteel.util.ObjectUtils;

@Service("worksNumCountService")
public class WorksNumCountServiceImpl implements WorksNumCountService
{
	@Autowired
	private WorksNumCountDao worksNumCountDao;

	@Override
	public void updateWNC(WorksNumCount worksNumCount)
	{
		Assert.notNull (worksNumCount);
		
		long memberId = worksNumCount.getMemberId  ();
		
		WorksNumCount wnc = getItsByMemberIed (memberId);
		if(ObjectUtils.notNull (wnc))
		{
			wnc.setAlbumNum (worksNumCount.getAlbumNum ());
			wnc.setInvationNum (worksNumCount.getInvationNum ());
			wnc.setMusicNum (worksNumCount.getMusicNum ());
			wnc.setVideoNum (worksNumCount.getVideoNum ());
			wnc.setWeiboNum (worksNumCount.getWeiboNum ()); 
			
			worksNumCountDao.update (wnc);
		}else if(ObjectUtils.isNull (wnc))
		{
			worksNumCountDao.save  (worksNumCount);
		}
	}

	@Override
	public void updateWorksNumCount(long memberId, String workName,String meth)
	{
		WorksNumCount wnc = getItsByMemberIed (memberId);
		
		if (ObjectUtils.isNull (wnc))
		{
			wnc = new WorksNumCount ();
			wnc.setAlbumNum (0);
			wnc.setInvationNum (0);
			wnc.setMemberId (memberId);
			wnc.setMusicNum (0);
			wnc.setVideoNum (0);
			wnc.setWeiboNum (0);
		}
			if (meth.equals ("add"))
			{
				if (workName.equals ("weibo"))
				{
					wnc.setWeiboNum (wnc.getWeiboNum () + 1);
					
				}else if (workName.equals ("music"))
				{
					wnc.setMusicNum (wnc.getMusicNum () + 1);
					
				}else if (workName.equals ("video"))
				{
					wnc.setVideoNum (wnc.getVideoNum () + 1);
					
				}else if (workName.equals ("invation"))
				{
					wnc.setInvationNum (wnc.getInvationNum () + 1);
					
				}
			}else
			{
				if (workName.equals ("weibo")&&wnc.getWeiboNum ()>0)
				{
					wnc.setWeiboNum (wnc.getWeiboNum () -1);
					
				}else if (workName.equals ("music")&&wnc.getMusicNum ()>0)
				{
					wnc.setMusicNum (wnc.getMusicNum () -1);
					
				}else if (workName.equals ("video")&&wnc.getVideoNum ()>0)
				{
					wnc.setVideoNum (wnc.getVideoNum () - 1);
					
				}else if (workName.equals ("invation")&&wnc.getInvationNum ()>0)
				{
					wnc.setInvationNum (wnc.getInvationNum () - 1);
					
				}
			}
			
			updateWNC(wnc);
	}

	@Override
	public WorksNumCount getItsByMemberIed(long memberId)
	{
		
		return worksNumCountDao.getByMemberId (memberId);
	}

	@Override
	public void deleteItsByMemberId(long memberId)
	{
		WorksNumCount wnc = getItsByMemberIed (memberId);
		
		if(ObjectUtils.notNull (wnc))
		{
			
			worksNumCountDao.delete (memberId); 
		}
	}

	@Override
	public void saveWNC(WorksNumCount worksNumCount)
	{
		Assert.notNull (worksNumCount);
		
		worksNumCountDao.save (worksNumCount);
	}

	@Override
	public long getAllWorkNum(long memberId)
	{
		WorksNumCount wnc = worksNumCountDao.getByMemberId (memberId);
		
		long all = 0;
		
		if(ObjectUtils.notNull (wnc))
		{
		  all  = wnc.getAlbumNum ()+wnc.getInvationNum ()+wnc.getMusicNum ()+wnc.getVideoNum ()+wnc.getWeiboNum ();
		}
		
		return all;
	}
}
